use std::sync::{Arc, Mutex};
use std::thread;

fn main() {
    //创建一个共享的可变整数
    let counter = Arc::new(Mutex::new(0));
    //创建一个多线程增加计数器
    let mut handles = vec![];

    for _ in 0..5 {
        let counter = Arc::clone(&counter);
        let handle = thread::spawn(move || {
            //获取锁
            let mut num = counter.lock().unwrap();
            *num += 1;
            //手动释放
            drop(num);
        });
        handles.push(handle);
    }

    //等待所有线程执行完成
    for handle in handles {
        handle.join().unwrap();
    }
    //打印最终计算器值
    println!("Final count: {}", *counter.lock().unwrap());
}
